package com.simple.portal.dao.impl;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Repository;

import com.simple.base.dao.BaseDAO;
import com.simple.portal.dao.IUserInfoDAO;
import com.simple.portal.vo.GroupInfoVO;
import com.simple.portal.vo.OrganizationVO;
import com.simple.portal.vo.UserInfoVO;
import com.simple.util.common.Constants;
import com.simple.util.common.StringUtil;
import com.simple.util.object.PageInfoBean;

@Repository("userInfoDAO")
public class UserInfoDAOImpl extends BaseDAO implements IUserInfoDAO {

	@Override
	public UserInfoVO findById(final String id) {
		return  super.get(UserInfoVO.class,id);
	}

	@Override
	public UserInfoVO save(final UserInfoVO userInfoVO,final String operator) {
		UserInfoVO tempUserInfoVO = super.save(userInfoVO);
		super.saveLogger(operator, tempUserInfoVO,tempUserInfoVO.getUserId(), Constants.LOGGER_OPERATE_FLAG_ADD);
		return tempUserInfoVO;
	}
	
	@Override
	public void update(final UserInfoVO userInfoVO,final String type,final String operator) {
		super.update(userInfoVO);
		super.saveLogger(operator, userInfoVO,userInfoVO.getUserId(), type);
	}

	@Override
	public List<UserInfoVO> findAll() {
		return super.query(UserInfoVO.class);
	}

	@Override
	public void delete(final UserInfoVO userInfoVO,final String operator) {
		super.delete(userInfoVO);
		super.saveLogger(operator, userInfoVO,userInfoVO.getUserId(),Constants.LOGGER_OPERATE_FLAG_DELETE);
	}

	@Override
	public void delete(final String id,final String operator) {
		UserInfoVO userInfoVO =findById(id);
		super.delete(UserInfoVO.class, id);
		super.saveLogger(operator, userInfoVO, userInfoVO.getUserId(), Constants.LOGGER_OPERATE_FLAG_DELETE);
	}

	
	public UserInfoVO findByLoginId(final String loginId){
		
		 final StringBuffer sb = new StringBuffer(300);
	        
	        sb.append( "select u from UserInfoVO  u where  u.loginId=?" );

			return  (UserInfoVO)super.get(sb.toString(), loginId);

	}
	
	@Override
	public List<UserInfoVO> findNullPasswordList() {
		return super.query("select uvo from UserInfoVO uvo where uvo.status=1 and uvo.password is null ");
	}

	
	@Override
	public OrganizationVO findOrganizationVOByLoginId(final String loginId){
		
		 final StringBuffer sb = new StringBuffer(300);
	        
	        sb.append( "select org  from UserInfoVO  u ,OrganizationVO  org  where org.organizationId=u.organizationId and u.loginId=?" );

			return  (OrganizationVO)super.get(sb.toString(), loginId);
		
	}
	@Override
	public GroupInfoVO findGroupInfoVOByLoginId(final String loginId){
		
		 final StringBuffer sb = new StringBuffer(300);
	        
	        sb.append( "select g  from UserInfoVO  u ,UserGroupRelVO urgr ,GroupInfoVO g  ");
	        sb.append("  where u.userId=urgr.userId  and  urgr.groupId=g.groupId ");
	        sb.append("and u.loginId=?");
			return  (GroupInfoVO)super.get(sb.toString(), loginId);
		
	}
	
	@Override
	public List<GroupInfoVO> findGroupInfoVOListByLoginId(final String loginId){
		 	final StringBuffer sb = new StringBuffer(300);
		 	List<String> paramList=new ArrayList<String>();
		 	paramList.add(loginId);
		 	//paramList.add(Constants.GROUP_LEVEL_ONE);
		 	
	        sb.append( "select g  from UserInfoVO  u ,UserGroupRelVO urgr ,GroupInfoVO g  ");
	        sb.append("  where u.userId=urgr.userId  and  urgr.groupId=g.groupId ");
	        sb.append("and u.loginId=?");
			return  super.query(sb.toString(), paramList);
		
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<UserInfoVO> findUserInfoByPage(String userId,
			String userIdSearch, String userName,
			String mobilePhone, String status,
			PageInfoBean pageInfoBean) {
		final StringBuffer sb = new StringBuffer(300);
	 	final Map<String, Object> paramMap = new LinkedHashMap<String,Object>();
	 	
        sb.append( "select u from UserInfoVO u where 1>0");
        sb.append(" and u.creator =:userId ");
	 	paramMap.put("userId", userId);
	 	
	 	if(!StringUtil.isNull(userIdSearch)){
            sb.append( " and u.userId like :userIdSearch" );
            paramMap.put("userIdSearch", "%"+userIdSearch+"%");
        }
	 	if(!StringUtil.isNull(userName)){
            sb.append( " and u.username like :userName" );
            paramMap.put("userName", "%"+userName+"%");
        }
	 	if(!StringUtil.isNull(mobilePhone)){
	 		sb.append( " and u.mobilePhone like :mobilePhone" );
	 		paramMap.put("mobilePhone", "%"+mobilePhone+"%");
	 	}
	 	if(!StringUtil.isNull(status)){
            sb.append( " and u.status=:status" );
            paramMap.put("status", status);
        }
	 	
	 	 sb.append( " order by u.createDate desc" );

		return  (List<UserInfoVO>) super.queryListByPage("u.userId", sb.toString(), paramMap, pageInfoBean);
	
	}


}
